(function($) {
	"use strict";
	$.fn.showInModal = function(options) { 

		var modal = {},
			matches = this,
			win = $(window);

		/* Options
		------------------------------------- */
		modal.options = jQuery.extend(true, {
			triggerEvent: "click",
			useCarousel: true,
			useSiblings: false,
			overlayId: "cm-overlay",
			overlayClass: null,
			overlayCss: {
				backgroundColor: "#000",
				position: "fixed",
				top: 0,
				left: 0,
				width: "100%",
				height: $("body").outerHeight(true),
				opacity: 0.7,
				zIndex: 1000
			},
			containerId: "cm-container",
			containerClass: null,
			containerCss: {
				padding: 10,
				backgroundColor: "#fff",
				position: "fixed",
				zIndex: 1001
			},
			contentId: "cm-content",
			contentClass: null,
			contentCss: {},
			contentHeader: null,
			contentFooter: null,
			minWidth: null,
			minHeight: null,
			maxWidth: null,
			maxHeight: null,
			initWidth: 460,
			initHeight: 320,
			autoResizeToFitInsideViewport: true,
			autoPositionOnResize: true,
			closeButton: "<a class=\"cm-closebutton\" title=\"Close\">Close</a>",
			prevButton: "<a class=\"cm-prevbutton\" title=\"Previous\">Previous</a>",
			nextButton: "<a class=\"cm-nextbutton\" title=\"Next\">Next</a>",
			closeModalOnEsc: true,
			closeOverlayOnClick: true,
			afterModalOpen: null,
			afterModalShow: null,
			afterModalClose: null
		}, options);

		/* Modal Carousel
		------------------------------------- */
		modal.carousel = {
			step: function(direction){
				
			}
		};

		/* Attach events to modal
		------------------------------------- */
		modal.attachEvents = function(){
			// Add resize and scrol events 
			// to update modal position
			win.bind("resize scroll", function(e){
				modal.window.update(e);
			});
			// Add click event on overlay 
			// to close it when clicked
			if(modal.options.closeOverlayOnClick){
				modal.window.overlay.bind("click", function(){
					modal.window.close();
				});
			}
			// Add the closebutton and attach close event
			modal.window.container.append($(modal.options.closeButton)).bind("click", function(){
				modal.window.close();
			});
			// Add prev- and next buttons and attach events
			// if options and matches count match
			if(modal.options.useCarousel && matches.length > 1){
				modal.window.container
					.append($(modal.options.prevButton).bind('click', function(){
						modal.carousel.step("previous");
					}))
					.append($(modal.options.nextButton).bind('click', function(){
						modal.carousel.step("next");
					}));
			}
		};

		/* Detach events from modal
		------------------------------------- */
		modal.detachEvents = function(){
			win.unbind("resize");
		};

		/* Get dimensions
		------------------------------------- */
		modal.getDimensions = function(elm){
			return {
				top: (win.height()/2) - (modal.options.initHeight/2) + ($("body").scrollTop()),
				left: (win.width()/2) - (modal.options.initWidth/2),
				width: modal.options.initWidth,
				height: modal.options.initHeight
			};
		};

		/* Modal window
		------------------------------------- */
		modal.window = {
			// Close modal window
			close: function(boolRunCallback){
				$("#"+ modal.options.overlayId).remove();
				$("#"+ modal.options.containerId).remove();
				modal.detachEvents();
				// Run event functions?
				if(boolRunCallback !== false && (typeof modal.options.afterModalClose === "function")){
					modal.options.afterModalClose();
				}
			},
			// Open modal window
			open: function(){
				// Add style and attributes to modal elements
				// Overlay
				this.overlay
					.attr("id", modal.options.overlayId)
					.addClass(modal.options.overlayClass)
					.css(modal.options.overlayCss);
				// Container
				this.container
					.attr("id", modal.options.containerId)
					.addClass(modal.options.containerClass)
					.css($.extend(modal.options.containerCss, modal.getDimensions()));
				// Content
				this.content
					.attr("id", modal.options.contentId)
					.addClass(modal.options.contentClass)
					.css(modal.options.contentCss);
				// Loading
				this.loading.css({
					position: "absolute",
					top: 0,
					left: 0,
					width: "100%",
					height: "100%",
					background: "url(gfx/loading.gif) no-repeat 50% 50%"
				})
				// Display modal
				this.overlay.appendTo($("body"));
				this.loading.prependTo(this.content);
				this.container.prepend(this.content).appendTo($("body"));
				// Run event functions?
				if(typeof modal.options.afterModalOpen === "function"){
					modal.options.afterModalOpen();
				}
			},
			// Update modal window size and positions
			update: function(e){
				e = (!e) ? window.event : e;
				// Only apply new position on scroll if the
				// element isnt position:fixed
				if(e.type === "scroll" && modal.options.containerCss.position !== "undefined" && modal.options.containerCss.position === "fixed"){
					return false;
				}
				// Apply new dimensions and reposition
				$("#"+ modal.options.containerId).css(modal.getDimensions());
			}
		};

		/* Init modal
		------------------------------------- */
		modal.init = function(e){
			e.preventDefault();
			modal.window.overlay = $("<div></div>");
			modal.window.container = $("<div></div>");
			modal.window.content = $("<div></div>");
			modal.window.loading = $("<div></div>");
			modal.window.close(false);
			modal.attachEvents();
			modal.window.open();
		};

		matches.unbind(modal.options.triggerEvent).bind(modal.options.triggerEvent, modal.init);

	};
})(jQuery);